package com.team9.fitness.controller;

import com.team9.fitness.service.ZRDDSSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;

/**
 * 帖子喜欢控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/post-favorite")
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
public class PostFavoriteController {

    @Autowired
    private ZRDDSSendService zrddsSendService;

    /**
     * 切换帖子喜欢状态
     */
    @PostMapping("/toggle")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> togglePostFavorite(@RequestParam Long userId,
            @RequestParam Long postId) {

        log.info("切换帖子收藏状态请求: userId={}, postId={}", userId, postId);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("userId", userId);
        requestBody.put("postId", postId);

        return zrddsSendService.sendBusinessRequest("POST_FAVORITE_TOGGLE", "POST", "/api/post-favorite/toggle",
                userId.toString(), requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "收藏状态切换成功");
                        result.put("data", response.getData());
                        log.info("收藏状态切换成功: userId={}, postId={}", userId, postId);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "收藏状态切换失败");
                        log.warn("收藏状态切换失败: userId={}, postId={}, error={}", userId, postId, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("收藏状态切换异常: userId={}, postId={}", userId, postId, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "收藏状态切换服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取用户喜欢状态
     */
    @GetMapping("/status")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getFavoriteStatus(@RequestParam Long userId,
            @RequestParam Long postId) {

        log.info("获取用户收藏状态请求: userId={}, postId={}", userId, postId);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("userId", userId);
        requestBody.put("postId", postId);

        return zrddsSendService.sendBusinessRequest("POST_FAVORITE_GET_STATUS", "GET", "/api/post-favorite/status",
                userId.toString(), requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取收藏状态成功");
                        result.put("data", response.getData());
                        log.info("获取收藏状态成功: userId={}, postId={}", userId, postId);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取收藏状态失败");
                        log.warn("获取收藏状态失败: userId={}, postId={}, error={}", userId, postId, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取收藏状态异常: userId={}, postId={}", userId, postId, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取收藏状态服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取帖子喜欢数量
     */
    @GetMapping("/count/{postId}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getFavoriteCount(@PathVariable Long postId) {

        log.info("获取帖子收藏数量请求: postId={}", postId);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("postId", postId);

        return zrddsSendService
                .sendBusinessRequest("POST_FAVORITE_GET_COUNT", "GET", "/api/post-favorite/count/" + postId,
                        null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取收藏数量成功");
                        result.put("data", response.getData());
                        log.info("获取收藏数量成功: postId={}", postId);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取收藏数量失败");
                        log.warn("获取收藏数量失败: postId={}, error={}", postId, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取收藏数量异常: postId={}", postId, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取收藏数量服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取用户收藏列表
     */
    @GetMapping("/user/{userId}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getUserFavorites(@PathVariable Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {

        log.info("获取用户收藏列表请求: userId={}, page={}, size={}", userId, page, size);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("userId", userId);
        requestBody.put("page", page);
        requestBody.put("size", size);

        return zrddsSendService.sendBusinessRequest("POST_FAVORITE_GET_USER_FAVORITES", "GET", "/api/post-favorite/user/" + userId,
                userId.toString(), requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取用户收藏列表成功");
                        result.put("data", response.getData());
                        log.info("获取用户收藏列表成功: userId={}, page={}, size={}", userId, page, size);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取用户收藏列表失败");
                        log.warn("获取用户收藏列表失败: userId={}, page={}, size={}, error={}", userId, page, size, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取用户收藏列表异常: userId={}, page={}, size={}", userId, page, size, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取用户收藏列表服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }
}
